package com.sxhuangjiang.formvalidating.controller;

import com.sxhuangjiang.formvalidating.entity.UserVO;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * @author huang
 * @version V1.0
 * @date 2019-12-03 15:38
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @GetMapping("/update")
    public String updateP(UserVO userVO){
        return "update";
    }

    @PostMapping("/update")
    @ResponseBody
    public String updateUser(@Valid UserVO userVO, BindingResult bindingResult){
        System.out.println(bindingResult.hasErrors());
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        fieldErrors.forEach(fieldError -> {
            System.out.println(fieldError.getField());
            System.out.println(fieldError.getRejectedValue());
            System.out.println(fieldError.getDefaultMessage());
        });
        return "ok";
    }
}
